﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Daphne.Forms
{
    /// <summary>
    /// Interaction logic for SelectLanguageForm.xaml
    /// </summary>
    public partial class SelectLanguageForm : Window
    {
        private string _langCode = null;

        /// <summary>
        /// Registered languages
        /// </summary>
        private List<LangRec> _langList = new List<LangRec>()
        {
            new LangRec() { LangTitle = "English", LangCode="en-US" },
            new LangRec() { LangTitle = "Česky", LangCode="cs-CZ" },
            new LangRec() { LangTitle = "Español (es-ES)", LangCode="es-ES" },
            new LangRec() { LangTitle = "Español (es-LA)", LangCode="es-MX" }
        };

        /// <summary>
        /// Constructor
        /// </summary>
        public SelectLanguageForm()
        {
            InitializeComponent();

            // bind languages
            uxLangs.ItemsSource = _langList;
            uxLangs.SelectedIndex = 0;

            uxSelect.Click += uxSelect_Click;
            uxCancel.Click +=uxCancel_Click;
        }


        /// <summary>
        /// Sets whether Cancel button is allowed
        /// </summary>
        public bool AllowCancel
        {
            get
            {
                return uxCancel.IsEnabled;
            }
            set
            {
                uxCancel.IsEnabled = value;
            }
        }
      
        /// <summary>
        /// Returns Form Result
        /// </summary>
        public bool Result
        { get; set; }

        /// <summary>
        /// Returs the selected language code
        /// </summary>
        public string LangCode
        {
            get
            {
                return _langCode;
            }
            set
            {
                _langCode = value;
                if (string.IsNullOrEmpty(LangCode) == false)
                {
                    uxLangs.SelectedValue = _langList.FirstOrDefault(i => i.LangCode == LangCode);
                }
            }
        }

        /// <summary>
        /// Select button handler
        /// </summary>
        void uxSelect_Click(object sender, RoutedEventArgs e)
        {
            object objLang = uxLangs.SelectedValue;
            if (objLang == null)
            {
                MessageBox.Show("Please, select your language.");
                return;
            }

            LangCode = (objLang as LangRec).LangCode;
            Result = true;
            this.Close();
        }

        /// <summary>
        /// Cancel button handler
        /// </summary>
        void uxCancel_Click(object sender, RoutedEventArgs e)
        {
            Result = false;
            this.Close();
        }

        /// <summary>
        /// Language Binding Class
        /// </summary>
        private class LangRec
        {
            public string LangTitle
            { get; set; }

            public string LangCode
            { get; set; }

            public override string ToString()
            {
                return LangTitle;
            }
        }
    }
}
